package com.young.api;

import com.github.pagehelper.PageInfo;
import com.young.annot.Log;
import com.young.domain.SysUser;
import com.young.req.AddUserReq;
import com.young.req.SysUserPageReq;
import com.young.res.LoginUserRes;
import com.young.shop.service.SysRoleService;
import com.young.shop.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 用户管理api
 * @Author: kangyang
 * @Date: 2021/5/14 15:06
 **/
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

  private static final Logger log = LoggerFactory.getLogger(SysUserController.class);

  @Autowired
  private SysUserService sysUserService;

  @Autowired
  private SysRoleService sysRoleService;

  @GetMapping("/info")
  @Log(name = "查询用户信息")
  public ResponseEntity getLoginUser() {
    log.info("查询当前管理员信息开始");
    SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
    LoginUserRes loginUserRes = new LoginUserRes();
    loginUserRes.setUserId(user.getUserId());
    loginUserRes.setUsername(user.getUsername());

    log.info("查询当前管理员信息开始，响应参数->{}", loginUserRes);
    return ResponseEntity.ok(loginUserRes);
  }

  @GetMapping("/info/{userId}")
  public ResponseEntity getUserInfo(@PathVariable Long userId) {
    log.info("查询用户信息开始，userId->{}", userId);
    SysUser user = sysUserService.getUserByUserId(userId);
    LoginUserRes loginUserRes = new LoginUserRes();
    List<Long> roleIdByUserId = sysRoleService.getRoleIdByUserId(userId);
    loginUserRes.setUserId(user.getUserId());
    loginUserRes.setUsername(user.getUsername());
    loginUserRes.setEmail(user.getEmail());
    loginUserRes.setMobile(user.getMobile());
    loginUserRes.setRoleIdList(roleIdByUserId);
    loginUserRes.setStatus(user.getStatus());
    log.info("查询用户信息结束，响应参数->{}", loginUserRes);
    return ResponseEntity.ok(loginUserRes);
  }

  @GetMapping("/page")
  public ResponseEntity<PageInfo<SysUser>> getSysUserPage(SysUserPageReq sysUserPageReq) {
    log.info("分页请求管理列表开始，请求参数：{}", sysUserPageReq);
    PageInfo<SysUser> sysUserPage = sysUserService.getSysUserPage(sysUserPageReq);
    log.info("分页请求管理列表结束，响应参数：{}", sysUserPage);
    return ResponseEntity.ok(sysUserPage);
  }

  @DeleteMapping
  @ApiOperation("删除用户")
  @Log(name = "删除用户")
  public Boolean deleteUser(@RequestBody List<Long> userIds) {
    log.info("删除用户开始， 请求参数{}", userIds);
    Boolean result = sysUserService.delectUserByUserIds(userIds);
    log.info("删除用户结束，响应参数{}", result);
    return result;
  }

  @PostMapping
  @ApiOperation("新增用户")
  @Log(name = "新增用户")
  public ResponseEntity<Boolean> addUser(@RequestBody AddUserReq addUserReq) {
    log.info("新增用户开始， 请求参数{}", addUserReq);
    SysUser principal = (SysUser) SecurityUtils.getSubject().getPrincipal();
    addUserReq.setCreateUserId(principal.getUserId());
    Boolean result = sysUserService.insertUser(addUserReq);
    log.info("新增用户结束，响应参数{}", result);
    return ResponseEntity.ok(result);
  }

  @PutMapping
  @ApiOperation("修改用户信息")
  @Log(name = "修改用户信息")
  public Boolean updateUser(@RequestBody AddUserReq addUserReq) {
    log.info("修改用户信息开始， 请求参数{}", addUserReq);
    Boolean result = sysUserService.updateUser(addUserReq);
    log.info("修改用户信息结束，响应参数{}", result);
    return result;
  }

}
